//
//  WDChapter3.c
//  ProjectM
//
//  Created by gaofeng on 2021/4/17.
//

#include "WDChapter3.h"
#include "YKStack.h"
void title4(void);
void title5(void);
void title2(void);
void TrainArrage(char *train);
int dc3(LinkStack L,int n);
void runChapter3(void){
    printf("WDChapter3\n");
    title5();
}
void title5(void){
    stk s;int f;
    initStk(&s);
//    printf("0: %d,1:%d \n",s.top[0],s.top[1]);
    stkopt op1={&s,0,3};
    stkopt op2={&s,1,4};
    f = pushStk(op1);
    f = pushStk(op2);
    op1.x = 4;
    f = pushStk(op1);
    op2.x = 9;
    f = pushStk(op2);
    op1.x = 5;
    f = pushStk(op1);
    op1.x = 6;
    f = pushStk(op1);
    op1.x = 0;
    //printf("index = %d\n",s.stack[5]);
    f = popStk(&op1);
//    printf("op.x = %d\n",s.stack[s.top[0]]);
        printf("op.x = %d\n",op1.x);
    if (f==0) {
        printf("2- %d \n",s.stack[2]);
    }
}
void title2(void){
    char train[] = "HHHSSHSHHSSS";
    TrainArrage(train);
    printf("%s \n",train);
}
void TrainArrage(char *train){
    char *p = train,*q=train,c;
    SqStack S;
    InitStack(&S);
 
    while (*p) {
        if (*p=='H') {
            Push(&S, *p);
        }else{
            *(q++)=*p;
        }
        p++;
    }
    while (!StackEmpty(&S)) {
        Pop(&S, &c);
        *(q++) = c;
    }
    
}
void title4(void){
    LinkStack L;
    Linknode n1,n2,n3;
    n1.data = 'x';  n1.next = &n2;
    n2.data = 'y';  n2.next = &n3;
    n3.data = 'x';  n3.next = NULL;
    L = &n1;
    int f = dc3(L, 3);
    printf("res = %d\n",f);
}
// 判断链表L是否中心对称
int dc3(LinkStack L,int n){
    int i;
    char s[10];
    Linknode *p = L;
    for (i=0; i<n/2; i++) {
        s[i]= p->data;
        p= p->next;
    }
    i--;
    if (n%2==1) {
        p=p->next;
    }
    while (p!=NULL && s[i]==p->data) {
        i--;
        p = p->next;
    }
    if (i==-1) {
        return 1;
    }else{
        return 0;
    }
}
